package Function;

import interfaces.DALException;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;


import dto_objects.UserDTO;

@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	public String loggedInMenuUser = "<div id=menu><br>"+
										"<span class=menu>Profile</span><br>"+
										"<span class=tab><a href=\"../User/fakturaUser.jsp\">View Loans</a></span><br>"+
										"<span class=tab><a href=\"../User/updateUser.jsp\">Update Profile</a></span><br>"+				
										"<br><br>"+
									  "</div>";
	public String loggedInMenuLibary = "<div id=menu><br>"+
										"<span class=menu>Books</span><br>"+
										"<span class=tab><a href=\"../Libary/createBook.jsp\">Create Book</a></span><br>"+
										"<span class=tab><a href=\"../Libary/updateBook.jsp\">Update Book</a></span><br>"+
										"<span class=tab><a href=\"../Libary/deleteBookCopy.jsp\">Delete Book</a></span><br>"+		
										"<span class=menu>Users</span><br>"+
										"<span class=tab><a href=\"../User/createUser.jsp\">Create User</a></span><br>"+
										"<span class=tab><a href=\"../User/updateUserLibary.jsp\">Update User</a></span><br>"+
										"<span class=tab><a href=\"../User/faktura.jsp\">User loans</span><br>"+
										"<br><br>"+
									  "</div>";
	public String loggedInMenuAdmin = "<div id=menu><br>"+
										"<span class=menu>Books</span><br>"+
										"<span class=tab><a href=\"../Libary/createBook.jsp\">Create Book</a></span><br>"+
										"<span class=tab><a href=\"../Libary/updateBook.jsp\">Update Book</a></span><br>"+
										"<span class=tab><a href=\"../Libary/deleteBookCopy.jsp\">Delete Book</a></span><br>"+
										"<span class=menu>Users</span><br>"+
										"<span class=tab><a href=\"../User/createUser.jsp\">Create User</a></span><br>"+
										"<span class=tab><a href=\"../User/updateUserAdmin.jsp\">Update User</a></span><br>"+
										"<span class=tab><a href=\"../User/faktura.jsp\">User loans</a></span><br>"+
										"<span class=menu>Admin</span><br>"+
										"<span class=tab><a href=\"../User/deleteUser.jsp\">Delete User</a></span><br>"+
										"<br><br>"+
									  "</div>";
							
	
    public LoginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String user = (request.getParameter("un"));
	    String pass = (request.getParameter("pw"));
	    //Debug
	    System.out.println("Debug - Login/Password");
	    System.out.println(user);
	    System.out.println(pass);
	    //---- 
	    HttpSession session = request.getSession(true);
	    String referer = request.getHeader("Referer");
	    
	    if(session.getAttribute("currentSessionUser") == null){
	    	session.removeAttribute("userMENU");
	    	System.out.println("into if statement");
		    FunctionWEB func = (FunctionWEB)session.getAttribute("func");
		    try{
		    	UserDTO userData = func.login(user, pass);
			    session.setAttribute("currentSessionUser", userData);
			    // user is logged in, getting the right function layer
			    session.removeAttribute("func");
			    String rights = userData.getRights();
			    if(rights.equals("user")){
			    	FunctionUSER funcLoggedIn = new FunctionUSER();
			    	session.setAttribute("func", funcLoggedIn);
			    	session.setAttribute("userMENU", loggedInMenuUser);
			    }else if(rights.equals("libary")){
			    	FunctionLIBRARY funcLoggedIn = new FunctionLIBRARY();
			    	session.setAttribute("func", funcLoggedIn);
			    	session.setAttribute("userMENU", loggedInMenuLibary);
			    }else if(rights.equals("admin")){
			    	FunctionADMIN funcLoggedIn = new FunctionADMIN();
			    	session.setAttribute("func", funcLoggedIn);
			    	session.setAttribute("userMENU", loggedInMenuAdmin);
				}
			    
			    //
			    response.sendRedirect(referer); //logged-in page 
		    }catch(DALException e){
		    	System.out.println(e);
		    	response.sendRedirect(referer); //logged-in page with error
		    }
	    }else{
	    	if(request.getParameter("logout") !=null){
	    		System.out.println("logout");
	    		session.removeAttribute("func");
	    		session.removeAttribute("currentSessionUser");
	    		session.removeAttribute("userMENU");
	    	}
	    	response.sendRedirect(referer);
	    }
	    
	    
	    

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
